﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace XHCTMS.VIEW
{
    public partial class Student : System.Web.UI.Page
    {

        
        private string[] parasValue = new string[] { };

        protected void Page_Load(object sender, EventArgs e)
        {          
            if (!IsPostBack)
            {
                try
                {
                    int page = new BLL.Student().MaxPage();
                    repStuInfoBind(new BLL.Student().GetPart(1), page);
                    Session["page"] = 1;
                    Session["maxPage"] = page;
                    Session["getFlag"] = "show";

                    this.ddlSubName.DataSource = new BLL.Sub().GetSubName();
                    this.ddlSubName.DataTextField = "sub_name";                   
                    this.ddlSubName.DataBind();
                    this.ddlSubName.Items.Insert(0, new ListItem("", ""));

                    this.ddlClassName.DataSource = new BLL.Class().GetClassName();
                    this.ddlClassName.DataTextField = "class_name";
                    this.ddlClassName.DataValueField = "class_id";
                    this.ddlAlterStuClassName.DataSource = this.ddlAddStuClassName.DataSource = new BLL.Class().GetAllClassName();
                    this.ddlAlterStuClassName.DataTextField = this.ddlAddStuClassName.DataTextField = "class_allname";
                    this.ddlAlterStuClassName.DataValueField = this.ddlAddStuClassName.DataValueField = "class_id";

                    this.ddlClassName.DataBind();
                    this.ddlAddStuClassName.DataBind();                   
                    this.ddlClassName.Items.Insert(0, new ListItem("", ""));
                    this.ddlAlterStuClassName.DataBind();

                    this.ddlSubYear.DataSource = new BLL.Sub().GetSubYears();
                    this.ddlSubYear.DataTextField  = "sub_years";
                    this.ddlSubYear.DataBind();
                    this.ddlSubYear.Items.Insert(0, new ListItem("", ""));

                    this.ddlClassTea.DataSource = new BLL.User().GetUserName();
                    this.ddlClassTea.DataTextField = "user_name";
                    this.ddlClassTea.DataBind();
                    this.ddlClassTea.Items.Insert(0, new ListItem("", ""));
                }
                catch
                {
                }

               
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
        
            int classID = 0;
            try
            {
                classID = int.Parse(this.txtTempStuClassID.Text);

                int result = new BLL.Student().Add(this.txtTempXHStuID.Text.Trim(), this.txtTempStuName.Text.Trim(),
                this.txtTempStuGender.Text.Trim(), this.txtTempStuPerID.Text.Trim(), classID);
                if (result == 1)
                {
                    
                }
                else
                {
                    Response.Write("<script type='text/javascript'>alert('注册失败');</script>");
                }

                switch (Session["getFlag"].ToString())
                {
                    case "show":
                        {
                            int page = int.Parse(Session["page"].ToString());
                            repStuInfoBind(new BLL.Student().GetPart(page), int.Parse(Session["maxPage"].ToString()));
                            this.ddlPage.SelectedIndex = page - 1;

                        }
                        break;

                    case "search":
                        {
                            int page = int.Parse(Session["page"].ToString());
                            repStuInfoBind(new BLL.Student().Search(parasValue, page), int.Parse(Session["maxPage"].ToString()));

                            this.ddlPage.SelectedIndex = page - 1;
                        }
                        break;
                }
            }
            catch
            {
                Response.Write("<script type='text/javascript'>alert('注册失败');</script>");
            }
            this.txtTempStuName.Text = null;
            this.txtTempStuPerID.Text = null;
            this.txtTempXHStuID.Text = null;     
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            parasValue = new string[]{txtXHStuID.Value.Trim(), txtStuName.Value.Trim(), ddlSubName.Text.Trim(),ddlSubYear.Text.Trim(),
                this.ddlClassName.SelectedItem.Text.Trim(), ddlClassTea.Text.Trim()};

           
            DataTable dt = new BLL.Student().Search(parasValue);
            int page = new BLL.Student().MaxPage(parasValue);

            repStuInfoBind(dt,page);
            Session["SearchArr"] = parasValue; 
            Session["page"] = 1;
            Session["maxPage"] = page;
            Session["getFlag"] = "search";
        }

        private void repStuInfoBind(DataTable dt,int maxPage)
        {
           
            List<string> pageList = new List<string>{};
            for(int i=1; i<= maxPage ;i++)
            {
                pageList.Add(i.ToString()+"/"+maxPage.ToString());
            }
            this.ddlPage.DataSource = pageList;
            this.ddlPage.DataBind();
            this.repStuInfo.DataSource = dt;
            this.repStuInfo.DataBind();
        }

        protected void btnAFirst_Click(object sender, EventArgs e)
        {
            switch (Session["getFlag"].ToString())
            {
                case "show":

                    repStuInfoBind(new BLL.Student().GetPart(1),int.Parse(Session["maxPage"].ToString()));
                    Session["page"] = 1;
                    this.ddlPage.SelectedIndex = 0;
                    break;
                case "search":
                    parasValue = (string[])Session["SearchArr"];
                    repStuInfoBind(new BLL.Student().Search(parasValue), int.Parse(Session["maxPage"].ToString()));
                    Session["page"] = 1;
                    this.ddlPage.SelectedIndex = 0;
                    break;
            }
        }

        protected void btnAPrevious_Click(object sender, EventArgs e)
        {
            switch (Session["getFlag"].ToString())
            {
                case "show":
                    {
                        int count = int.Parse(Session["page"].ToString()) - 1;
                        if (count > 0)
                        {
                            repStuInfoBind(new BLL.Student().GetPart(count), int.Parse(Session["maxPage"].ToString()));
                            Session["page"] = count;
                            this.ddlPage.SelectedIndex = count -1;
                        }
                    }
                    break;
                case "search":
                    {
                        parasValue = (string[])Session["SearchArr"];
                        int count = int.Parse(Session["page"].ToString()) - 1;
                        if (count > 0)
                        {
                            repStuInfoBind(new BLL.Student().Search(parasValue, count), int.Parse(Session["maxPage"].ToString()));
                            Session["page"] = count;
                            this.ddlPage.SelectedIndex = count -1;
                        }
                    }
                    break;
            }
        }

        protected void btnANext_Click(object sender, EventArgs e)
        {
            switch (Session["getFlag"].ToString())
            {
                case "show":
                    {
                        int count = int.Parse(Session["page"].ToString()) + 1;
                        if (count <= int.Parse(Session["maxPage"].ToString()))
                        {
                            repStuInfoBind(new BLL.Student().GetPart(count), int.Parse(Session["maxPage"].ToString()));
                            Session["page"] = count;
                            this.ddlPage.SelectedIndex = count -1;
                        }
                    }
                    break;
                case "search":
                    {
                        parasValue = (string[])Session["SearchArr"];
                        int count = int.Parse(Session["page"].ToString()) + 1;
                        if (count <= int.Parse(Session["maxPage"].ToString()))
                        {
                            repStuInfoBind(new BLL.Student().Search(parasValue,count), int.Parse(Session["maxPage"].ToString()));
                            Session["page"] = count;
                            this.ddlPage.SelectedIndex =count -1;
                        }
                    }
                    break;
            }
        }

        protected void btnALast_Click(object sender, EventArgs e)
        {
            switch (Session["getFlag"].ToString())
            {
                case "show":
                    {
                        int page = int.Parse(Session["maxPage"].ToString());
                        repStuInfoBind(new BLL.Student().GetPart(page),int.Parse(Session["maxPage"].ToString()));
                        Session["page"] = page;
                        this.ddlPage.SelectedIndex = page - 1;
                    }
                    break;
                    
                case "search":
                    {
                        parasValue = (string[])Session["SearchArr"];
                        int page = int.Parse(Session["maxPage"].ToString());
                        repStuInfoBind(new BLL.Student().Search(parasValue,page), int.Parse(Session["maxPage"].ToString()));
                        Session["page"] = page;
                        this.ddlPage.SelectedIndex = page -1;
                    }
                    break;
            }
        }

        protected void ddlPage_SelectedIndexChanged(object sender, EventArgs e)
        {
            int page = int.Parse(this.ddlPage.SelectedValue.Split('/')[0]);

            switch (Session["getFlag"].ToString())
            {
                case "show":
                    {                     
                        repStuInfoBind(new BLL.Student().GetPart(page), int.Parse(Session["maxPage"].ToString()));
                        Session["page"] = page;
                        this.ddlPage.SelectedIndex = page - 1;
                    
                    }
                    break;

                case "search":
                    {
                        parasValue = (string[])Session["SearchArr"];
                        repStuInfoBind(new BLL.Student().Search(parasValue, page), int.Parse(Session["maxPage"].ToString()));
                        Session["page"] = page;
                        this.ddlPage.SelectedIndex = page - 1;
                    }
                    break;
            }
        }

        protected void btnAlter_Click(object sender, EventArgs e)
        {
            int result = new BLL.Student().Alter(int.Parse(txtTempStuID.Text), txtTempXHStuID.Text, txtTempStuName.Text,
                int.Parse(txtTempStuClassID.Text), this.txtTempStuState.Text);
            if (result == 1)
            {
               
            }
            else
            {
                Response.Write("<script type='text/javascript'>alert('修改失败');</script>");
            }
            switch (Session["getFlag"].ToString())
            {
                case "show":
                    {
                        int page = int.Parse(Session["page"].ToString());
                        repStuInfoBind(new BLL.Student().GetPart(page), int.Parse(Session["maxPage"].ToString()));
                        this.ddlPage.SelectedIndex = page - 1;

                    }
                    break;

                case "search":
                    {
                        parasValue = (string[])Session["SearchArr"];
                        int page = int.Parse(Session["page"].ToString());
                        repStuInfoBind(new BLL.Student().Search(parasValue, page), int.Parse(Session["maxPage"].ToString()));

                        this.ddlPage.SelectedIndex = page - 1;
                    }
                    break;
            }
        }

        protected void btnGraduation_Click(object sender, EventArgs e)
        {
            int result = new BLL.Student().StuGraduation(int.Parse(this.txtTempStuID.Text));
        }



    }
}